<?
class Encrypt{

	var $algorithm;
	var $algorithm_directory;
	var $mode;
	var $mode_directory;
	var $encode_data;
	var $decode_data;
	var $td;
	
	function init_encrypt($algorithm,$algorithm_dir,$mode,$mode_dir)
	{
		
		$this->algorithm=$algorithm;
		$this->algorithm_directory=$algorithm_dir;
		$this->mode=$mode;
		$this->mode_directory=$mode_dir;
		$this->td = mcrypt_module_open($this->algorithm,$this->algorithm_directory,$this->mode,$this->mode_directory);
    }
	
	function get_key()
	{
		return $this->keyword;
	}
	
	function encode($keyword,$plain_text)
	{
		$key = substr($keyword, 0, mcrypt_enc_get_key_size($this->td));
    	$iv_size = mcrypt_enc_get_iv_size($this->td);
    	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$result=mcrypt_generic_init($this->td, $key, $iv);
		if($result==-1){
			return "error encryption initialization";
		}else{
			$this->encode_data = mcrypt_generic($this->td, $plain_text);
       //		mcrypt_generic_deinit($this->td);
		//	mcrypt_module_close($this->td);
			return trim($this->encode_data);
		}
	}
	
	function decode($keyword,$encrypted)
	{
		$key = substr($keyword, 0, mcrypt_enc_get_key_size($this->td));
    	$iv_size = mcrypt_enc_get_iv_size($this->td);
    	$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$result=mcrypt_generic_init($this->td, $key, $iv);
		//return $result;
		if($result==-1){
			return "error encryption initialization";
		}else{
			$this->decode_data=mdecrypt_generic($this->td, $encrypted);
        	mcrypt_generic_deinit($this->td);
        	mcrypt_module_close($this->td);
			return trim($this->decode_data);
		}
	}
}
?>